home *** CD-ROM | disk | FTP | other *** search
- Path: info.uah.edu!oreo!gbacon
- From: gbacon@oreo (Greg Bacon)
- Newsgroups: comp.lang.c
- Subject: Re: Help with simple code
- Date: 14 Jan 1996 22:35:07 GMT
- Organization: The University of Alabama in Huntsville
- Distribution: world
- Message-ID: <4dc0er$9ug@info.uah.edu>
- References: <4dbak5$oij@ionews.io.org>
- NNTP-Posting-Host: oreo.aspire.cs.uah.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- John Gordon MacPherson (jgordon@io.org) wrote:
- : Can anyone tell me what's wrong with this piece of code? I lifted it
- : straight from a textbook.
-
- : Here's the code:
-
- : /* Calculating compound interest */
- : #include <stdio.h>
- : #include <math.h>
-
- : main()
- : {
- : int year;
- : double amount, principal = 1000, rate = 0.5;
-
- : printf("%4s%21s\n", "Year", "Amount on deposit");
-
- : for (year = 1; year <= 10; year++) {
- : amount = principal * pow(1.0 + rate, year);
- : printf("%4d%21.2f\n", year, amount);
- : }
-
- : return 0;
- : }
- : ______________________________________________________________
-
- : and here's the error:
-
- : In function `main':
- : undefined reference to `pow'
-
- : I don't understand. `pow' is a function, not a variable?
- : Can anyone tell me what's wrong?
-
- In addition, you need to check your compiler's documentation to see
- whether you need to link against any external libraries. Something
- else that you should be aware of is that linkers see functions and
- variables as basically the same thing. Depending on your compiler,
- functions themselves are nothing special, just pointers themselves.
- Others necessitate that a pointer to function "be 'turned into' a
- 'real' function with the * operator" (see question 4.12 of the FAQ).
-
- It all depends on how one's viewpoint, but a real philosophical
- question (yes, philosophy is very much a part of C programming) is
- "what is the true nature of functions?" :)
-
- Food for thought,
- Greg
- --
- Greg Bacon <gbacon@cs.uah.edu>
- University of Alabama in Huntsville
- CS Department Systems Support Team
-